//
//  HLDetailNodeView.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/11.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLDetailNodeView.h"
#import <Masonry/Masonry.h>
#import "HttpRequestManager.h"
#import "MBProgressHUD+HL.h"
#import "HLUserInfo.h"

@interface HLDetailNodeView()

/** NoneNoteImageView  */
@property (nonatomic ,strong) UIImageView *noneNoteImageView;
/** createNoteButton  */
@property (nonatomic ,strong) UIButton *createNoteButton;
/** 内容文字  */
@property (nonatomic ,strong) UIView *content;
/** 编辑按钮  */
@property (nonatomic ,strong) UIButton *editButton;
/** 删除按钮  */
@property (nonatomic ,strong) UIButton *deleteButton;
/** 分割线  */
@property (nonatomic ,weak) UIView *line;

@end

@implementation HLDetailNodeView

- (UIButton *)createNoteButton
{
    if (!_createNoteButton) {
        _createNoteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _createNoteButton.backgroundColor = kColorMineBlueBG;
        _createNoteButton.layer.cornerRadius = 17.5;
        _createNoteButton.clipsToBounds = YES;
        [_createNoteButton setTitle:@"创建笔记" forState:UIControlStateNormal];
        [_createNoteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_createNoteButton addTarget:self action:@selector(clickCreteNote:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _createNoteButton;
}

- (UIImageView *)noneNoteImageView
{
    if (!_noneNoteImageView) {
        _noneNoteImageView = [[UIImageView alloc] init];
        _noneNoteImageView.image = [UIImage imageNamed:@"home_course_nonenote"];
    }
    return _noneNoteImageView;
}

- (UIView *)content
{
    if (!_content) {
        _content = [[UIView alloc] init];
        _content.backgroundColor = [UIColor clearColor];
        _content.userInteractionEnabled = YES;
        
        UITextView *contentTextView = [[UITextView alloc]init];
        contentTextView.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 14:(kMainTitleFontSize + 2)];
        contentTextView.textColor = HLRGB(60, 60, 60);
        contentTextView.backgroundColor = [UIColor whiteColor];
        contentTextView.userInteractionEnabled = NO;
        [_content addSubview:contentTextView];
        
        UIView *bottomEdit = [[UIView alloc] init];
        bottomEdit.backgroundColor = [UIColor whiteColor];
        [_content addSubview:bottomEdit];
        
        UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
        editButton.backgroundColor = kColorMineBlueBG;
        editButton.layer.cornerRadius = 15.0f;
        editButton.clipsToBounds = YES;
        [editButton setTitle:@"编辑笔记" forState:UIControlStateNormal];
        [editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [editButton addTarget:self action:@selector(editContent:) forControlEvents:UIControlEventTouchUpInside];
        self.editButton = editButton;
        [bottomEdit addSubview:editButton];
        
        UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        deleteButton.backgroundColor = kColorMineBlueBG;
        deleteButton.layer.cornerRadius = 15.0f;
        deleteButton.clipsToBounds = YES;
        [deleteButton setTitle:@"删除笔记" forState:UIControlStateNormal];
        [deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [deleteButton addTarget:self action:@selector(deleteContent:) forControlEvents:UIControlEventTouchUpInside];
        self.deleteButton = deleteButton;
        [bottomEdit addSubview:deleteButton];
        
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = HLRGB(235, 235, 235);
        self.line = line;
        [bottomEdit addSubview:line];

    }
    return _content;
}

- (instancetype)initWithFrame:(CGRect)frame courseID:(NSString *)courseID
{
    if (self = [super initWithFrame:frame]) {
        _course_id = courseID;
        
        [self loadRequestData];
    }
    return self;
}

- (void)setUI
{
    [self addSubview:self.noneNoteImageView];
    [self addSubview:self.createNoteButton];
    
    [self.noneNoteImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(self.height * 0.5));
        make.centerX.equalTo(self);
        make.width.equalTo(@(self.height * 0.5 * 0.84));
        make.top.equalTo(self).offset(self.height * 0.1);
    }];
    
    [self.createNoteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(35));
        make.centerX.equalTo(self);
        make.width.equalTo(@(self.width * 0.4));
        make.top.equalTo(self.noneNoteImageView.mas_bottom).offset(self.height * 0.1);
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
}

- (void)setNote:(NSString *)note
{
    _note = note;
    [self.createNoteButton removeFromSuperview];
    [self.noneNoteImageView removeFromSuperview];
    
    [self addSubview:self.content];
    [self configContengCons];
    
    UITextView * contentTextView = self.content.subviews[0];
    contentTextView.text = note;
}

- (void)loadRequestData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sign"] = [kHLRequestGetMyNotest signStr];
    params[@"course_id"] = self.course_id;
    params[@"user_id"] = [HLUserInfo account].userid;
    __weak typeof(self) weakSelf = self;
    [[HTTPRequestManager defaultManager] HTTPGetRequestWithApiName:kHLRequestGetMyNotest parameters:params success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject){
        if ([responseObject[@"code"] integerValue] == 200) {
            NSArray *array = responseObject[@"data"];
            if (array.count > 0) {
                NSString *noteStr = responseObject[@"data"][0][@"note"];
                if (noteStr && noteStr.length > 0) {
                    /**** 之前存在笔记 ****/
                    [weakSelf addSubview:weakSelf.content];
                    [weakSelf configContengCons];
                    
                    UITextView * contentTextView = weakSelf.content.subviews[0];
//                    contentTextView.text = [noteStr stringByRemovingPercentEncoding];
                    contentTextView.text = noteStr;
                }else{
                    [weakSelf setUI];
                }
            }else{
                [weakSelf setUI];
            }
        }else{
          [weakSelf setUI];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [weakSelf setUI];
        [MBProgressHUD showErrorMessage:@"网络超时"];
    }];
}

- (void)configContengCons
{
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.bottom.equalTo(self).offset(0);
    }];
    
    UITextView * contentTextView = self.content.subviews[0];
    [contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.content).offset(0);
        make.left.equalTo(self.content).offset(10);
        make.right.equalTo(self.content).offset(-10);
        make.bottom.equalTo(self.content).offset(-54);
    }];
    
     UIView * editView = self.content.subviews[1];
    [editView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(44));
        make.left.equalTo(self.content).offset(0);
        make.right.equalTo(self.content).offset(0);
        make.bottom.equalTo(self.content).offset(0);
    }];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(30));
        make.centerY.equalTo(editView);
        make.left.equalTo(@(ScreenWidth * 0.575));
        make.width.equalTo(@(ScreenWidth * 0.35));
    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(30));
        make.centerY.equalTo(editView);
        make.left.equalTo(@(ScreenWidth * 0.075));
        make.width.equalTo(@(ScreenWidth * 0.35));
        
    }];
    

    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(1));
        make.left.equalTo(editView).offset(0);
        make.right.equalTo(editView).offset(0);
        make.bottom.equalTo(editView).offset(0);
    }];
    
}

- (void)clickCreteNote:(UIButton *)button
{
    if ([_delegate respondsToSelector:@selector(detailNodeView:didClickCreteNote:)]) {
        [_delegate detailNodeView:self didClickCreteNote:button];
    }
}

- (void)editContent:(UIButton *)button
{
    UITextView * contentTextView = self.content.subviews[0];
    if ([_delegate respondsToSelector:@selector(detailNodeView:editNote:)]) {
        [_delegate detailNodeView:self editNote:contentTextView.text];
    }
}
- (void)deleteContent:(UIButton *)button
{
    UITextView * contentTextView = self.content.subviews[0];
    if ([_delegate respondsToSelector:@selector(detailNodeView:deleteNote:)]) {
        [_delegate detailNodeView:self deleteNote:contentTextView.text];
    }
}


@end
